package com.sjm.bookshop.controller.userctroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjm.bookshop.entity.User;
import com.sjm.bookshop.service.userservice.UserService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //    查询用户列表
    @RequestMapping("/selectAllUser")
    public Result getUser(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<User> userList = userService.userList();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return Result.success(200, pageInfo, "success");
    }

    //    模糊查询
    @RequestMapping("/selectByKeyWord")
    public Result getUser(HttpServletRequest request, String keyWord, int page, int limit) {

        if (keyWord.equals(null) || keyWord != null) {
            PageHelper.startPage(page, limit);
            List<User> userList = userService.selectSomeOne(keyWord);
            PageInfo<User> pageInfo = new PageInfo<>(userList);
            return Result.success(200, pageInfo.getList(), "success");
        } else {
            return Result.fail(201, "关键字不能为空");
        }
    }

    //    删除用户
    @RequestMapping("/deleteById/{id}")
    public Result deleteUser(@PathVariable("id") int id) {
        if (id != 0) {
            int res = userService.deleteOne(id);
            if (res > 0) {
                return Result.success(200, "成功删除" + res + "条记录");
            } else {
                return Result.fail(201, "删除失败");
            }
        } else {
            return Result.fail(201, "id不能为空");
        }

    }


    //    修改状态
    @RequestMapping("/change")
    public Result changeStatus(Integer isLocked, Integer id) {
        if (isLocked == 0 || isLocked == 1) {
            int res = userService.changeUserStatus(isLocked, id);
            if (res > 0) {
                return Result.success(200, "修改成功");
            } else {
                return Result.fail(201, "修改失败");
            }
        }
        return Result.fail(201, "修改失败");
    }

    //    修改用户
    @RequestMapping("/Update")
    public Result changeUserInfo(@RequestBody User user) {
        if (user != null) {
//            User(id=3, name=李华, sex=null, email=lihua@qq.com, phone=13828614126, password=6094B959CBE18DE8ACDC8EB027FB7742, userpic=null, isLocked=null, createdtime=null, updatedtime=Sun Oct 16 20:26:19 CST 2022)
            if (userService.UpdateUserInfo(user) > 0) {
                return Result.success(200, "成功修改了一条记录");
            } else {
                return Result.fail(201, "修改失败");
            }
        } else {
            return Result.fail(201, "修改失败");
        }
    }

}
